package com.dormitory_15.dormitory.Controller.DormitoryManager;

import com.dormitory_15.dormitory.Entity.DormitoryManager;
import com.dormitory_15.dormitory.Service.DormitoryManager.DormitoryMangerService;
import com.dormitory_15.dormitory.Utils.CommonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/dorManager")
public class DormitoryManagerController {
    @Autowired
    private DormitoryMangerService dormitoryMangerService;

    @PostMapping("/login")
    @ResponseBody
    public CommonResult login(@RequestBody DormitoryManager dormitoryManager, HttpSession session){
        try{
            String username = dormitoryManager.getUsername();
            String password = dormitoryManager.getPassword();
            if(username == null || password == null){
                return CommonResult.fail(400,"用户名或密码不能为空");
            }
            DormitoryManager userInfo = dormitoryMangerService.login(username, password);
            if(userInfo == null){
                return CommonResult.fail(400, "账号或密码错误,或用户不存在");
            }
            session.setAttribute("userInfo", userInfo);
            return CommonResult.success(userInfo);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.fail(500, e.getMessage());
        }
    }
    @GetMapping("/logout")
    @ResponseBody
    public CommonResult logout(HttpSession session){
        try{
            session.removeAttribute("userInfo");
            return CommonResult.success("退出成功");
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.fail(500, e.getMessage());
        }
    }

}
